package com.fs.adapters;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.studentmanagersystem.AdminClassesManageActivity;
import com.example.studentmanagersystem.R;
import com.example.studentmanagersystem.URL;
import com.fs.po.Classes;
import com.fs.utils.CmdUtils;

public class ClassesAdapter extends BaseAdapter {

	
	private Context context;
	private List<Classes> classList;
	private int len=0;
	
	Handler handler;
	
	public ClassesAdapter(Context context,Handler handler) {
		this.context=context;
		this.handler=handler;
	}
	
	public void setAdapterData(List<Classes> classList) {
		this.classList=classList;
		this.len=classList.size();
		System.out.println("List长度"+len);
		
	}
	
	/**
	 * checkAll选中所有复选框
	 */
	public void checkAll(boolean isChecked) {
	 
		
	}
	
	
	
	@Override
	public int getCount() {
		return len;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	int i;
	@Override
	public View getView(int position, View view, ViewGroup parent) {
		
		i=position;
		
		ViewHolder viewHolder=null;
		if(view==null){
			view = ((Activity) context).getLayoutInflater().inflate(R.layout.classeslist_tab, null);
			viewHolder = new ViewHolder();// 创建内部类对象，保存列表项中子控件的引用；
			viewHolder.nameText=(TextView) view.findViewById(R.id.classeslist_name);
			viewHolder.idText=(TextView) view.findViewById(R.id.classeslist_id);
			viewHolder.checkbox=(CheckBox) view.findViewById(R.id.classeslist_checkbox);
			 
			viewHolder.button=(Button) view.findViewById(R.id.classeslist_button);
			
			view.setTag(viewHolder);
		}else if (view != null) {
			   viewHolder=(ViewHolder) view.getTag();
		  }
			viewHolder.nameText.setText(""+this.classList.get(position).getName());
			
			viewHolder.idText.setText(""+this.classList.get(position).getClass_id());
			
			viewHolder.button.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					 
					//当点击这个按钮时，就会删除对应的课程
					showNormalDia();
					
				}
			});
			
			return view;
	}
	
	
	  class ViewHolder {
		  TextView nameText,idText;
		  CheckBox checkbox;
		  Button button;
		 }
	  
	  /**
	   * 弹出提示对话框
	   */
	  private void showNormalDia(){  
	        //AlertDialog.Builder normalDialog=new AlertDialog.Builder(getApplicationContext());  
	        AlertDialog.Builder normalDia=new AlertDialog.Builder(context);  
	        normalDia.setIcon(R.drawable.ic_launcher);  
	        normalDia.setTitle("提示");  
	        normalDia.setMessage("确认删除？");  
	          
	        normalDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
	            @Override  
	            public void onClick(DialogInterface dialog, int which) {  
	                
	            	String cmd="delete from class where _id="+classList.get(i).getClass_id()+"";
					
					System.out.println(cmd);
					
					CmdUtils cmdUtils=new CmdUtils(context,handler);
					
					cmdUtils.submitCmd(URL.CMD_URL,"delete",cmd);
					
	            }  
	        });  
	        
	        normalDia.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
	            @Override  
	            public void onClick(DialogInterface dialog, int which) {  
	               
	            }  
	        });  
	        normalDia.create().show();  
	    }  
}
